#pragma once

#include "ISampleProcessor.h"

namespace Microsoft {
	namespace Media {
		namespace FragmentedMediaSink {

			class AudioSampleProcessor : public ISampleProcessor
			{
			private:
				long long					_maxFragmentDuration;
				long long					_minDuration;
				long long					_currentFragmentDuration;
				long long					_currentFragmentOffset;
				long long					_fragmentEndTime;
				long long					_nextSampleTime;
				bool						_isInitialized;
				unsigned int				_samplesInFragment;
				unsigned int				_fragmentIndex;

				unsigned int				_sampleRate;
				unsigned int				_channels;
				unsigned int				_bitsPerSample;

			private:
				HRESULT InitializeFragments();
				HRESULT TerminateFragments();
				HRESULT WriteSample(IMFSample* spSample);
				HRESULT CreateBlankSample(IMFSample** ppSample, long long sampleTime, long long sampleDuration);
				HRESULT CreateBlankBuffer(IMFMediaBuffer** ppSample, long long duration);

			public:
				AudioSampleProcessor(Windows::Foundation::Collections::IVector<Microsoft::Media::FragmentedMediaSink::IFragmentController^>^ fragmentControllers, IMFMediaType* spInputMediaType, unsigned int inputStreamIndex);

				virtual ~AudioSampleProcessor();

				virtual HRESULT ProcessSample(IMFSample* spSample);

				virtual HRESULT SendTick(long long timeStamp);

				virtual HRESULT Finalize()
				{
					return TerminateFragments();
				}
			};

		}
	}
}